package com.danciyixue.boot.client.auth.controller;

import com.danciyixue.boot.common.enums.LogModuleEnum;
import com.danciyixue.boot.common.result.Result;
import com.danciyixue.boot.client.auth.service.ClientAuthService;
import com.danciyixue.boot.core.security.model.AuthenticationToken;
import com.danciyixue.boot.common.annotation.Log;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 客户端认证控制层
 */
@Tag(name = "01. 认证中心")
@RestController
@RequestMapping("/api/client/v1/auth")
@RequiredArgsConstructor
@Slf4j
public class ClientAuthController {

    private final ClientAuthService clientAuthService;

    @Operation(summary = "客户端登录")
    @PostMapping("/login")
    @Log(value = "客户端登录", module = LogModuleEnum.LOGIN)
    public Result<AuthenticationToken> login(
            @Parameter(description = "用户名", example = "admin") @RequestParam String username,
            @Parameter(description = "密码", example = "123456") @RequestParam String password
    ) {
        String sanitizedUsername = username == null ? null : username.trim();
        AuthenticationToken authenticationToken = clientAuthService.login(sanitizedUsername, password);
        clientAuthService.ensureActivationForCurrentUser();
        return Result.success(authenticationToken);
    }

    @Operation(summary = "客户端注销登录")
    @DeleteMapping("/logout")
    @Log(value = "客户端注销", module = LogModuleEnum.LOGIN)
    public Result<?> logout() {
        clientAuthService.logout();
        return Result.success();
    }
} 
